export GCC := gcc
export OBJTOOL := /root/linux-rv-livepatch/tools/objtool/objtool
export OBJDUMP := objdump
export OBJ_FLAG := -d
export GCC_FLAG := -c

ifdef flag
  ifneq (,$(findstring org_gcc,$(flag)))
    # 优化等级 -On
    GCC_FLAG += -O0
    # 使用压缩指令
    # GCC_FLAG += -march=rv64imadc
    # 无兄弟函数优化 -fno-optimize-sibling-calls
    GCC_FLAG += -fno-optimize-sibling-calls
    # 禁用跳表 -fno-jump-tables
    # 强制生成跳表 -fjump-tables
    # GCC_FLAG += -fno-jump-tables
    GCC_FLAG += -fjump-tables
  endif
  ifneq (,$(findstring org_obj,$(flag)))
    # objdump显示原始数据
    OBJ_FLAG += -M no-aliases
    # numeric 显示寄存器编号
    # OBJ_FLAG += -numeric
    # 显示所有段的头部信息
    # OBJ_FLAG += -h
    # 查看 -j <段>
    # OBJ_FLAG += -j .rodata
  endif
endif

SCENES := basic_stack recursive multi_file if_else unreachable c_ld snake_game jump_table_static jump_table_dynamic
SCENE ?= $(SCENES)

all: build_scenes

build_scenes: $(SCENE)

$(SCENE):
	@echo "Building scene: $@"
	$(MAKE) -C $@

objdump: export TARGET := objdump
objdump: build_scenes

dump: export TARGET := dump
dump: build_scenes 

orc: export TARGET := orc
orc: build_scenes

clean:
	@for scene in $(SCENES); do \
		$(MAKE) -C $$scene clean; \
	done
	rm -rf *.o *.txt

.PHONY: all clean build_scenes $(SCENES) objdump dump orc
